package com.futureCloudMOM.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.futureCloudMOM.core.base.service.impl.BaseServiceImpl;
import com.futureCloudMOM.core.domain.BasicBillType;
import com.futureCloudMOM.core.domain.BasicBillrule;
import com.futureCloudMOM.core.mapper.BasicBillTypeMapper;
import com.futureCloudMOM.core.mapper.BasicBillruleMapper;
import com.futureCloudMOM.core.service.BasicBillruleService;
import com.futureCloudMOM.core.vo.BasicBillRuleVO;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 【basic_billRule(单据规则表)】的数据库操作Service实现
 *
 * @author
 * @since 2023-08-31 14:18:56
 */
@AllArgsConstructor
@Service
public class BasicBillruleServiceImpl extends BaseServiceImpl<BasicBillruleMapper, BasicBillrule> implements BasicBillruleService {

    // 单据规则表 mapper
    private final BasicBillruleMapper basicBillruleMapper;
    // 订单类型
    private final BasicBillTypeMapper basicBillTypeMapper;

    @Override
    public List<BasicBillRuleVO> getList(QueryWrapper<BasicBillRuleVO> queryWrapper) {
        return basicBillruleMapper.getList(queryWrapper);
    }

    @Override
    public String getNumber(Long billId, Long InvoiceMainId, Long InvoiceSubId) {
        StringBuilder strNumber = new StringBuilder();
        // 根据选中的单据规则ID查询单据规则
        BasicBillrule basicBillrule = basicBillruleMapper.selectOne(new LambdaQueryWrapper<BasicBillrule>()
                .eq(BasicBillrule::getDeleted, 0)
                .eq(BasicBillrule::getBillTypeId, billId));
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        // 拿到当前日期格式化后的字符串
        String thisDate = simpleDateFormat.format(new Date());

        if (ObjectUtils.isNotEmpty(basicBillrule)) {
            // 处理隔天流水号清零
            if (ObjectUtils.isNotEmpty(basicBillrule.getOutPutNumber())) {
                // 拿到上次生成的日期 yyyyMMddHHmmss格式
                String serialDate = basicBillrule.getOutPutNumber().substring(basicBillrule.getPrefix().length(), basicBillrule.getPrefix().length() + thisDate.length());
                SimpleDateFormat s = new SimpleDateFormat("yyyyMMdd");
                // 格式转换 yyyyMMdd
                serialDate = serialDate.substring(0, serialDate.length() - 6);
                // 日期变了 当前流水号重置 从1开始
                if (!s.format(new Date()).equals(serialDate)) {
                    basicBillrule.setThisNumber(1L);
                }
            } else {
                basicBillrule.setThisNumber(1L);
            }
        } else {
            throw new RuntimeException("单据规则未查到,请维护该订单类型的单据规则");
        }

        // 拼接单据规则  前缀 + 日期时间 + 业务订单主表ID + 业务订单子表ID + 流水号
        strNumber.append(basicBillrule.getPrefix()).append(thisDate).append(InvoiceMainId).append(InvoiceSubId).append(basicBillrule.getThisNumber());
        basicBillrule.setThisNumber(basicBillrule.getThisNumber() + 1);
        basicBillrule.setOutPutNumber(strNumber.toString());
        basicBillruleMapper.updateById(basicBillrule);
        return strNumber.toString();
    }

    @Override
    public List<BasicBillType> getListByRuleBillTypeId(BasicBillType basicBillType) {
        List<BasicBillType> list = new ArrayList<>();
        List<BasicBillrule> basicBillrules = basicBillruleMapper.selectList(new LambdaQueryWrapper<BasicBillrule>()
                .eq(BasicBillrule::getDeleted, 0)
                .select(BasicBillrule::getBillTypeId));
        if (basicBillrules.size() > 0) {
            List<Long> ids = basicBillrules.stream().map(BasicBillrule::getBillTypeId).collect(Collectors.toList());
            list = basicBillTypeMapper.selectList(new LambdaQueryWrapper<BasicBillType>()
                    .eq(BasicBillType::getDeleted, 0)
                    .notIn(!ids.isEmpty(), BasicBillType::getId, ids)
                    .like(Objects.nonNull(basicBillType.getBillType()), BasicBillType::getBillType, basicBillType.getBillType())
                    .eq(Objects.nonNull(basicBillType.getInOutFlag()), BasicBillType::getInOutFlag, basicBillType.getInOutFlag()));
        } else {
            list = basicBillTypeMapper.selectList(new LambdaQueryWrapper<BasicBillType>()
                    .eq(BasicBillType::getDeleted, 0)
                    .like(Objects.nonNull(basicBillType.getBillType()), BasicBillType::getBillType, basicBillType.getBillType())
                    .eq(Objects.nonNull(basicBillType.getInOutFlag()), BasicBillType::getInOutFlag, basicBillType.getInOutFlag()));
        }
        return list;
    }
}
